﻿//进行基础数据配置
mview.debug = ConfigDebug;
mview.socket_url = ConfigSocket;
mview.stream_url = StreamSocket;
//这里读取并创建用户Token
if (localStorage.getItem("token") == null) {
    localStorage.setItem("token", mview.guid());
}
mview.token = localStorage.getItem("token");
mview.view.defaulturl = "/views/home/main.html";
mview.vue.default.header_display = false;
mview.vue.default.header_ok_display = false;
mview.vue.default.header_exit_name = '返回';
mview.vue.default.header_ok_name = '确定';
mview.vue.default.header_exit_click = function () {
    mview.view.back();
}
mview.vue.default.header_ok_click = function () { }
//自定义顶部菜单
mview.vue.header = new Vue({
    el: 'header',
    data: {
        header_display: false,
        header_title_name: '主页',
        header_exit_name: '返回',
        header_ok_display: false,
        header_ok_name: '确定'
    },
    methods: {
        header_exit_click: function () { },
        header_ok_click: function () { }
    }
});
//自定义底部菜单
mview.vue.default.footer_display = true;
mview.vue.footer = new Vue({
    el: 'footer',
    data: {
        footer_display: true,
        footer_choose: "home"
    },
    methods: {}
});
//当页面创建发生
mview.view.oncreate = function (pagedata) {
    //重写推送捕获事件
    mview.view.onpush = mview.vue.default.onpush;
    if (pagedata.screen.footer_display != undefined) { mview.vue.footer.$data.footer_display = pagedata.screen.footer_display; }
    if (pagedata.screen.footer_choose != undefined) { mview.vue.footer.$data.footer_choose = pagedata.screen.footer_choose; }
    if (pagedata.screen.header_display != undefined) { mview.vue.header.$data.header_display = pagedata.screen.header_display; }
    if (pagedata.screen.header_ok_display != undefined) { mview.vue.header.$data.header_ok_display = pagedata.screen.header_ok_display; }
    if (pagedata.screen.header_title_name != undefined) { mview.vue.head.$data.title = mview.vue.header.$data.header_title_name = pagedata.screen.header_title_name; }
    if (pagedata.screen.header_exit_name != undefined) { mview.vue.header.$data.header_exit_name = pagedata.screen.header_exit_name; }
    if (pagedata.screen.header_exit_click != undefined) { mview.vue.header.header_exit_click = pagedata.screen.header_exit_click; }
    if (pagedata.screen.header_ok_name != undefined) { mview.vue.header.$data.header_ok_name = pagedata.screen.header_ok_name; }
    if (pagedata.screen.header_ok_click != undefined) { mview.vue.header.header_ok_click = pagedata.screen.header_ok_click; }
}
//当发生错误时发生
mview.view.onerror = function (error) {
    mview.view.load("/views/shared/error.html", {
        message: error
    });
}
//当页面离开发生
mview.view.onclose = function () {
    mview.vue.header.$data.header_display = mview.vue.default.header_display;
    mview.vue.header.$data.header_ok_display = mview.vue.default.header_ok_display;
    mview.vue.header.$data.header_exit_name = mview.vue.default.header_exit_name;
    mview.vue.header.$data.header_ok_name = mview.vue.default.header_ok_name;
    mview.vue.header.header_exit_click = mview.vue.default.header_exit_click;
    mview.vue.header.header_ok_click = mview.vue.default.header_ok_click;
    mview.vue.footer.$data.footer_display = mview.vue.default.footer_display;
}
//当请求数据发生
mview.view.onrequest = function (data, guid) {
    //如果没有登录
    if (data.Code == - 30004) {
        mview.vue.header.$data.header_display = false;
        mview.vue.footer.$data.footer_display = false;
        mview.view.load("/views/user/poster.html", {});
        return null;
    }
    //如果没有实名
    else if (data.Code == - 30005) {
        //预处理一下页面
        mview.vue.header.$data.header_display = false;
        mview.vue.footer.$data.footer_display = false;
        mview.view.load("/views/user/real.html", {});
        return null;
    }
    else {
        return data.Data;
    }
}
//当数据刷新时发生
mview.view.onreload = function (data, guid) {
    //如果没有登录
    if (data.Code == - 30004) {
        mview.view.load("/views/user/poster.html", {});
        return null;
    }
    //如果没有实名
    else if (data.Code == - 30005) {
        mview.view.load("/views/user/real.html", {});
        return null;
    }
    else {
        return data.Data;
    }
}
//当页面推送发生
mview.socket.onpush = function (data) {
    //如果页面有自定义推送
    var fexec = false;
    if (mview.view.pagedata && mview.view.pagedata.onpush) {
        fexec = mview.view.pagedata.onpush(data);
        if (fexec == undefined) {
            fexec = false;
        }
    }
    //如果没有页面级调用 或者 页面级忽略
    if (fexec == false) {
        //如果是点对点消息推送
        if (data.DataType == "FRIEND_MESSAGE") {
            mview.control.tips("[" + data.Content.sendid + "]" + mview.custom.messageanalyze(data.Content), 3000);
        }
    }
}
//退出程序
mview.exit = function () {
    mview.socket.send({
        data: {
            method: "USER_EXIT"
        },
        success: function (data) {
            if (data.Code == 0) {
                m.cache.set('password', '');
                mview.view.load(mview.view.defaulturl);
            }
            else {
                alert(data.Message);
            }
        }
    });
}
//扫码执行
mview.app.scan = function (callback) {
    jsBridge.scan({
        needResult: true,
    }, function (code) {
        if (code) {
            //进行扫码解析
            var codes = code.split(':');
            if (codes.length <= 1) {
                var url = codes[0];
                if (url.indexOf(location.host) != -1) {
                    m.view.redirect(url)
                }
            }
            else {
                callback(codes[0].toUpperCase(), codes[1]);
            }
        } else {
            mview.control.tips("扫码失败或取消了扫码", 3000);
        }
    });
}
//自定义方法
mview.custom = {
    //发送短信验证码
    sms_every_send: function (elm, moblie, country) {
        if (elm.issend && elm.issend == true) {
            return;
        }
        else {
            elm.issend = true;
            mview.socket.send({
                data: {
                    method: "SMS_EVERY_SEND",
                    country: country,
                    moblie: moblie
                },
                success: function (data) {
                    if (data.Code == 0) {
                        mview.control.tips("发送成功");
                        if (elm.time) {
                            window.clearInterval(elm.time);
                        }
                        elm.oldvalue = elm.value;
                        elm.time_sec = 30;
                        elm.time = setInterval(function () {
                            elm.value = elm.time_sec + "S";
                            if (elm.time_sec <= 0) {
                                window.clearInterval(elm.time);
                                elm.value = elm.oldvalue;
                                elm.issend = false;
                            }
                            elm.time_sec -= 1;
                        }, 1000);
                    }
                    else {
                        mview.control.tips(data.Message);
                        elm.issend = false;
                    }
                }
            });
        }
    },
    //发送邮箱验证码
    mail_every_send: function (elm, email) {
        if (elm.issend && elm.issend == true) {
            return;
        }
        else {
            elm.issend = true;
            mview.socket.send({
                data: {
                    method: "MAIL_EVERY_SEND",
                    email: email
                },
                success: function (data) {
                    if (data.Code == 0) {
                        mview.control.tips("发送成功");
                        if (elm.time) {
                            window.clearInterval(elm.time);
                        }
                        elm.oldvalue = elm.value;
                        elm.time_sec = 30;
                        elm.time = setInterval(function () {
                            elm.value = elm.time_sec + "S";
                            if (elm.time_sec <= 0) {
                                window.clearInterval(elm.time);
                                elm.value = elm.oldvalue;
                                elm.issend = false;
                            }
                            elm.time_sec -= 1;
                        }, 1000);
                    }
                    else {
                        mview.control.tips(data.Message);
                        elm.issend = false;
                    }
                }
            });
        }
    },
    //发送短信验证码
    sms_only_send: function (elm) {
        if (elm.issend && elm.issend == true) {
            return;
        }
        else {
            elm.issend = true;
            mview.socket.send({
                data: {
                    method: "SMS_ONLY_SEND"
                },
                success: function (data) {
                    if (data.Code == 0) {
                        mview.control.tips("发送成功");
                        if (elm.time) {
                            window.clearInterval(elm.time);
                        }
                        elm.oldvalue = elm.value;
                        elm.time_sec = 30;
                        elm.time = setInterval(function () {
                            elm.value = elm.time_sec + "S";
                            if (elm.time_sec <= 0) {
                                window.clearInterval(elm.time);
                                elm.value = elm.oldvalue;
                                elm.issend = false;
                            }
                            elm.time_sec -= 1;
                        }, 1000);
                    }
                    else {
                        mview.control.tips(data.Message);
                        elm.issend = false;
                    }
                }
            });
        }
    },
    //发送邮箱验证码
    mail_only_send: function (elm) {
        if (elm.issend && elm.issend == true) {
            return;
        }
        else {
            elm.issend = true;
            mview.socket.send({
                data: {
                    method: "MAIL_ONLY_SEND"
                },
                success: function (data) {
                    if (data.Code == 0) {
                        mview.control.tips("发送成功");
                        if (elm.time) {
                            window.clearInterval(elm.time);
                        }
                        elm.oldvalue = elm.value;
                        elm.time_sec = 30;
                        elm.time = setInterval(function () {
                            elm.value = elm.time_sec + "S";
                            if (elm.time_sec <= 0) {
                                window.clearInterval(elm.time);
                                elm.value = elm.oldvalue;
                                elm.issend = false;
                            }
                            elm.time_sec -= 1;
                        }, 1000);
                    }
                    else {
                        mview.control.tips(data.Message);
                        elm.issend = false;
                    }
                }
            });
        }
    },
    //设置地址
    set_place: function (selt, root, callback) {
        mview.socket.send({
            data: {
                method: "PLACE_INFO",
                root: root
            },
            success: function (data) {
                var json = data.Data;
                selt.html("");
                selt.append("<option value=''>请选择</option>");
                for (var i in json) {
                    selt.append("<option value='" + json[i].id + "'>" + json[i].name + "</option>");
                }
                if (callback) { callback(); }
            }
        });
    },
    set_sexs: function (selt, callback) {
        mview.socket.send({
            data: {
                method: "SEXS_INFO"
            },
            success: function (data) {
                var json = data.Data;
                selt.html("");
                selt.append("<option value=''>请选择</option>");
                for (var i in json) {
                    selt.append("<option value='" + json[i].id + "'>" + json[i].name + "</option>");
                }
                if (callback) { callback(); }
            }
        });
    },
    set_trade: function (selt, callback) {
        mview.socket.send({
            data: {
                method: "TRADE_INFO"
            },
            success: function (data) {
                var json = data.Data;
                selt.html("");
                selt.append("<option value=''>请选择</option>");
                for (var i in json) {
                    selt.append("<option value='" + json[i].id + "'>" + json[i].name + "</option>");
                }
                if (callback) { callback(); }
            }
        });
    },
    //压缩图片
    preview_image: function (file, width , callback) {
        lrz(file.files[0], { width: width }).then(function (rst) {
            rst.formData.append('base64img', rst.base64);
            callback(rst.base64);
        }).catch(function (err) {
            mview.control.tips("当前浏览器不支持");
        }).always(function () { })
    },
    //聊天内容解析
    messageanalyze: function (data) {
        if (data.style == 1) {
            var str = data.content;
            str = str.replace(/{FS}/g, "<img class='face-img' src='/Images/Chat/i_f");
            str = str.replace(/{FE}/g, ".gif' />");
            return str;
        }
        else {
            return "[图片]";
        }
    },
    timeanalyze: function (hisTime) {
        if (!arguments.length) return '';
        var arg = arguments,
            now = arg[1] ? arg[1] : new Date().getTime(),
            diffValue = now - new Date(arg[0]),
            result = '',

            minute = 1000 * 60,
            hour = minute * 60,
            day = hour * 24,
            halfamonth = day * 15,
            month = day * 30,
            year = month * 12,

            _year = diffValue / year,
            _month = diffValue / month,
            _week = diffValue / (7 * day),
            _day = diffValue / day,
            _hour = diffValue / hour,
            _min = diffValue / minute;

        if (_year >= 1) result = parseInt(_year) + "年前";
        else if (_month >= 1) result = parseInt(_month) + "个月前";
        else if (_week >= 1) result = parseInt(_week) + "周前";
        else if (_day >= 1) result = parseInt(_day) + "天前";
        else if (_hour >= 1) result = parseInt(_hour) + "个小时前";
        else if (_min >= 1) result = parseInt(_min) + "分钟前";
        else result = "刚刚";
        return result;
    },
    dateDiff: function (startTime, endTime) {
        var startTime = new Date(startTime);
        var endTime = new Date(endTime);

        var diff = parseInt((startTime.getTime() - endTime.getTime()) / 1000 / 60);
        return diff;
    }
}
mview.start();